package com.CST.controller.zx;

import cn.hutool.core.bean.BeanUtil;
import com.CST.bo.zx.ProjectDetailBO;
import com.CST.bo.zx.ProjectSelectBO;
import com.CST.common.api.ResultObject;
import com.CST.dto.zx.ProjectDetailDTO;
import com.CST.dto.zx.ZxProjectSelectDetailDTO;
import com.CST.service.zx.IZxSumMoneyService;
import com.CST.service.zx.ZXProjectSelectService;
import com.CST.util.PageHelperTool;
import com.CST.util.ProjectTimeUtil;
import com.CST.vo.zx.*;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * @Author 伍文毅
 * @Date 2023 03 20 11 46
 **/
@Api(tags = {"项目查询"})
@RestController
@RequestMapping("/zxProjectSelect")
public class ZXProjectSelectController {
    @Resource
    private ZXProjectSelectService zxProjectSelectService;
    @Resource
    private IZxSumMoneyService zxSumMoneyService;
    @PostMapping("/queryForPage")
    @ApiOperation("分页显示首页数据")
    public ResultObject queryForPage(@RequestBody ProjectSelectBO projectSelectBO){
        ZxProjectSelectDetailDTO zxProjectSelectDetailDTO = zxProjectSelectService.queryForPage(projectSelectBO);
        return ResultObject.success(zxProjectSelectDetailDTO);
    }

    @PostMapping("/downloadAllProject")
    @ApiOperation("下载外部文件，必要参数HttpServletResponse response,下载的话第几页，每页几条信息就不要了,日期必须要")
    public void downloadFile(@RequestBody ProjectSelectBO projectSelectBO, HttpServletResponse response) throws Exception {
        zxProjectSelectService.downloadFile(projectSelectBO,response);
    }

    @PostMapping("/queryAllProjectDetailOn")
    @ApiOperation("查询明细信息，模态窗口上半部分页面")
    public ResultObject queryAllProjectDetailOn(@RequestBody ProjectDetailBO projectDetailBO) {
        projectDetailBO = ProjectTimeUtil.setProjectDetailBO(projectDetailBO);
        //        获取页面上方信息
        projectDetailBO.setStartNextMonthTime(projectDetailBO.getStartTime().with(TemporalAdjusters.firstDayOfNextMonth()));
        projectDetailBO.setEndNextMonthTime(projectDetailBO.getEndTime().with(TemporalAdjusters.firstDayOfNextMonth()));
        ZxFinicialDetailVO zxFinicialDetailVO = zxSumMoneyService.queryFinicialDetail(projectDetailBO);
        Map<String,Object> map = new HashMap<>();
        map.put("high",zxFinicialDetailVO);
        map.put("down",zxFinicialDetailVO);
        return ResultObject.success(map);
    }

    @PostMapping("/queryAllProjectDetailDown")
    @ApiOperation("查询明细信息，模态窗口下半部分页面")
    public ResultObject queryAllProjectDetail(@RequestBody ProjectDetailBO projectDetailBO) {
        projectDetailBO = ProjectTimeUtil.setProjectDetailBO(projectDetailBO);
        List<ProjectDetailVO> list = zxSumMoneyService.queryAllProjectDetail(projectDetailBO);
//        获取页面下方明细信息
        PageInfo<ProjectDetailVO> projectDetailVOPageInfo = PageHelperTool.excutePageInfo(list, projectDetailBO.getPageNo(), projectDetailBO.getPageSize());
        ProjectDetailDTO projectDetailDTO = new ProjectDetailDTO();
        projectDetailDTO.setPageInfo(projectDetailVOPageInfo);
        List<ProjectDetailVO> projectDetailVOList = list;
        BigDecimal countMoney = BigDecimal.ZERO;

        //收入类型列表
        List<Long> incomeTypeList = new ArrayList<>();

        //支出类型列表
        List<Long> payoutTypeList = new ArrayList<>();

        //其他类型列表
        List<Long> otherList =new ArrayList<>();

        Collections.addAll(incomeTypeList, 100301L, 100302L);
        Collections.addAll(payoutTypeList, 100303L, 100304L);
        Collections.addAll(otherList,9999L);
        if (projectDetailVOList.size() != 0) {
            if(projectDetailBO.getExpenseTypeCode() ==null)
            {
                //全部
                BigDecimal incomeTotal = projectDetailVOList.stream().filter(t -> incomeTypeList.contains(t.getReExTypeCode())).map(t -> t.getBalance()).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal payoutTotal = projectDetailVOList.stream().filter(t -> payoutTypeList.contains(t.getReExTypeCode())).map(t -> t.getBalance()).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal otherTotal = projectDetailVOList.stream().filter(t -> otherList.contains(t.getReExTypeCode())).map(t -> t.getBalance()).reduce(BigDecimal.ZERO, BigDecimal::add);
                countMoney =incomeTotal.subtract(payoutTotal).subtract(otherTotal);
            }
            else {
                //支出
                countMoney = projectDetailVOList.stream().map(t -> t.getBalance()).reduce(BigDecimal.ZERO, BigDecimal::add);
            }
        }

        projectDetailDTO.setSumMoney(countMoney);
        return ResultObject.success(projectDetailDTO);
    }

    @PostMapping("/downloadAllDetailProject")
    @ApiOperation("下载内部文件，必要参数HttpServletResponse response,下载的话第几页，每页几条信息就不要了,日期必须要")
    public void downloadDetailFile(@RequestBody ProjectDetailBO projectDetailBO, HttpServletResponse response) throws Exception {
        projectDetailBO = ProjectTimeUtil.setProjectDetailBO(projectDetailBO);
        zxSumMoneyService.downloadDetailFile(projectDetailBO,response);
    }
}
